/*
* Copyright 2005-2009 Jesse Andrews
*
* This file may be used under the terms of of the
* GNU General Public License Version 3 or later (the "GPL"),
* http://www.gnu.org/licenses/gpl.html
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*/

/***************
 * ISBN Helpers
 */

var ISBN = {
  trim: function(isbn) {
    return isbn.toLowerCase().replace(/-/g,'').replace(/\s/g,'');
  },
  checksum10: function(isbn9) {
    var isbn = ISBN.trim(isbn9);

    if (isbn.match(/^\d{9}$/)) {
      var checksum = 0;
      for (var i=0; i<9; i++) {
        checksum += isbn9[i] * (i+1);
      }
      checksum = checksum % 11;
      if (checksum == 10) {
        checksum = 'x';
      }

      return checksum;
    }
  },
  validate10: function(isbn10) {
    var isbn = ISBN.trim(isbn10);

    if (isbn.match(/^\d{9}[0-9x]$/)) {
      if (isbn[9] == ISBN.checksum10(isbn.slice(0,9))) {
        return isbn;
      }
    }
  },
  checksum13: function(isbn12) {
    var isbn = ISBN.trim(isbn12);

    if (isbn.match(/^\d{12}$/)) {
      var checksum = 0;
      for (var i=0; i<12; i++) {
        checksum += isbn[i] * (i%2 == 0 ? 1 : 3);
      }

      return (10 - (checksum % 10)) % 10;
    }
  },
  validate13: function(isbn13) {
    var isbn = ISBN.trim(isbn13);

    if (isbn.match(/^\d{13}$/)) {
      if (isbn13[12] == ISBN.checksum13(isbn.slice(0,12))) {
        return isbn;
      }
    }
  },
  validate: function(isbn) {
    isbn = ISBN.trim(isbn);

    if (isbn.length == 13) {
      return ISBN.validate13(isbn);
    }
    if (isbn.length == 10) {
      return ISBN.validate10(isbn);
    }
  },
  convert: function(isbnIn) {
    var isbn;
    if (isbn = ISBN.validate13(isbnIn)) {
      if (isbn.slice(0,3) != '978') {
        return; // perhaps return the ISBN13 instead of null?
      }

      isbn = isbn.slice(3,12);
      return isbn + ISBN.checksum10(isbn);
    }

    if (isbn = ISBN.validate10(isbnIn)) {
      isbn = '978' + isbn.slice(0,9);
      return isbn + ISBN.checksum13(isbn);
    }
  },
  replace: function(url, isbn) {
    return url.replace('#{ISBN}', isbn.toLowerCase())
      .replace('#{ISBN_UPCASE}', isbn.toUpperCase());
  },
  test: function(isbns) {
    if (!isbns) {
      isbns = ['9992158107','99921-58-10-7','9971502100','9604250590','0943396042','080442957x',
               '097522980x','9780306406157','9780940016736','9780940016613'];
    }
    for (var i=0; i<isbns.length; i++) {
      var isbn = ISBN.validate(isbns[i]);
      if (!isbn) {
        return 'fail validating ' + isbns[i];
      }
      if (isbn.length == 10) {
        if (!ISBN.validate13(ISBN.convert(isbn))) {
          return 'fail converting ' + isbn;
        }
      }
      else {
        if (isbn.slice(0,3) == '978' && !ISBN.validate10(ISBN.convert(isbn))) {
          return 'fail converting ' + isbn;
        }
      }
    }

    return 'pass';
  },

}

/*
if (DEBUG) {
  log("Testing ISBN: " + ISBN.test());
}
*/